package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class SignoffServlet extends HttpServlet {

    private static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";

    private Account account;
    private List<Product> myList;
    private boolean authenticated;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        //持久化购物车
        session.setAttribute("username", null);
        session.setAttribute("cart", null);
        System.out.println(session.getAttribute("username"));

        //清空session
//        session.invalidate();
        //重新赋值
        account = new Account();
        myList = null;
        authenticated = false;
        //放入session
        session.setAttribute("account", account);
        session.setAttribute("myList", myList);
        session.setAttribute("authenticated", authenticated);

        //设置浏览器不要读取本地缓存，防止前端页面和后端服务器的session值不同
        response.setHeader("Cache-Control","no-store");
        response.setHeader("Pragma","no-cache");
        response.setDateHeader("Expires",0);

        request.getRequestDispatcher(MAIN).forward(request, response);
    }
}
